package com.mohan.json;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mohan.pojo.Article;
import com.mohan.pojo.Comment;
import com.mohan.pojo.Content;
import com.mohan.pojo.Img;
import com.mohan.pojo.Reply;
import com.mohan.service.ArticleService;
import com.mohan.util.ArticleRep;
import com.mohan.util.CommentRep;
import com.mohan.util.ContentRep;

public class ArticleBuild {
	public Object buildOneArticle(Article article, List<Content> contents, List<Comment> comments, ArticleService articleService){
		ArticleRep articleRep = new ArticleRep();
		articleRep.setArticleId(article.getArticleId());
		articleRep.setAuthor(article.getAuthor());
		articleRep.setBook(article.getBook());
		articleRep.setDynasty(article.getDynasty());
		articleRep.setArticleProfile(article.getArticleProfile());
		articleRep.setTags(article.getTags());
		articleRep.setStyle(article.getStyle());
		articleRep.setType(article.getType());
		articleRep.setLoves(article.getLoves());
		articleRep.setAnalysis(article.getAnalysis());
		articleRep.setBookId(article.getBookId());
		articleRep.setUserid(article.getUserId());
		articleRep.setExamine(article.getExamine());
		articleRep.setArticleStatus(article.getArticleStatus());
		articleRep.setRecommend(article.getRecommend());
		articleRep.setIsOpen(article.getIsOpen());
		articleRep.setTitle(article.getTitle());
		
		/**
		 * 获取content
		 */
		List<ContentRep> contentReps = new ArrayList<ContentRep>();
		for (Content content : contents) {
			ContentRep contentRep = new ContentRep();
			contentRep.setContentId(content.getContentId());
			contentRep.setColor(content.getFontColor());
			contentRep.setSize(content.getFontSize());
			contentRep.setAudioUrl(content.getAudioUrl());
			contentRep.setVideoUrl(content.getVideoUrl());
			contentRep.setSerialNum(content.getSerialNum());
			contentRep.setContent(content.getContent());
			
			/**
			 * 获取插图
			 */
			List<Img> imgList = new ArrayList<Img>();
			List<Img> imgs;
			try {
				imgs = articleService.getImgs(content.getContentId());
				for (Img img : imgs) {
					imgList.add(img);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			
			contentRep.setImgs(imgList);
			
			contentReps.add(contentRep);
		}
		articleRep.setContents(contentReps);
		
		/**
		 * 获取comment
		 */
		List<CommentRep> commentReps = new ArrayList<CommentRep>();
		for (Comment comment : comments) {
			CommentRep commentRep = new CommentRep();
			commentRep.setCommentId(comment.getCommentId());
			commentRep.setUserid(comment.getUserId());
			commentRep.setName(comment.getName());
			commentRep.setLoves(comment.getLoves());
			commentRep.setContent(comment.getContent());
			commentRep.setSerialNum(comment.getSerialNum());			
			List<Reply> relpyList = new ArrayList<Reply>();
			List<Reply> replys;
			try {
				replys = articleService.getReplys(comment.getCommentId());
				for (Reply reply : replys) {
					relpyList.add(reply);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			commentRep.setReplys(relpyList);
			
			commentReps.add(commentRep);
		}
		articleRep.setComments(commentReps);
		
		return articleRep;
	}
}
